package com.example.xlmkotlinwanandroid.model.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.xlmkotlinwanandroid.base.BaseViewModel
import com.example.xlmkotlinwanandroid.model.bean.ArticleBean
import com.example.xlmkotlinwanandroid.model.repository.Repository
import com.example.xlmkotlinwanandroid.model.bean.ProjectCategoryBean
import com.example.xlmkotlinwanandroid.model.repository.ProjectRepository
import com.example.xlmkotlinwanandroid.network.Network.getProjectCategory
import kotlinx.coroutines.launch
import java.lang.Exception

class ProjectViewModel : BaseViewModel() {
    //设置
    private var categoryLiveData = MutableLiveData<ArrayList<ProjectCategoryBean>>()
    private var projectArticleLiveData = MutableLiveData<ArrayList<ArticleBean>>()

    private var pageNo = 1 //设置页码
    fun getProjectCategory(): MutableLiveData<ArrayList<ProjectCategoryBean>> {
        viewModelScope.launch {
            try {
                val categoryList = Repository.getProjectCategory()
                categoryList?.let {
                    categoryLiveData.postValue(it)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }
        return categoryLiveData
    }

    fun getProjectArticleByType(
        isRefresh: Boolean,
        cid: Int
    ): MutableLiveData<ArrayList<ArticleBean>> {
        if (isRefresh) {
            pageNo = 1
        }else{
            pageNo++
        }
        viewModelScope.launch {
            try {
                val projectArticleList = Repository.getProjectArticleByType(pageNo, cid)
                projectArticleList?.let {
                    projectArticleLiveData.postValue(it)
                }

            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        return projectArticleLiveData
    }
}